package com.pphh.demo.common;

import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.samplers.ConstSampler;
import io.jaegertracing.internal.samplers.ProbabilisticSampler;
import io.jaegertracing.spi.Reporter;
import io.jaegertracing.spi.Sampler;
import io.opentracing.util.GlobalTracer;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TraceRegister {

    /**
     * 注册全局tracer
     */
    public static void initTracer(String appName, Double samplingRate) {
        log.info("initialized the global tracer");

        Reporter reporter = new TraceReporter();
        Sampler sampler = null;
        if (samplingRate >= 0.0D && samplingRate <= 1.0D) {
            sampler = new ProbabilisticSampler(samplingRate);
        } else {
            sampler = new ConstSampler(true);
        }
        JaegerTracer tracer = new JaegerTracer.Builder(appName)
                .withReporter(reporter)
                .withSampler(sampler)
                .build();
        GlobalTracer.registerIfAbsent(tracer);
    }

}
